package com.example.demo.day0304;

public class TestState {
    public static void main(String[] args) {
        DemoState t = new DemoState();
        final Thread thread = new Thread(t);
        System.out.println(thread.getName() + " state after constructor:"
                + thread.getState());//start之前的状态是NEW
        try {
            Thread.sleep(1000L);
            thread.start();
            Thread.sleep(1000L);
            System.out.println(thread.getName() + " state after run:"
                    + thread.getState());//结束的状态为TERMINATED
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Thread thread1 = new Thread(new Runnable() {
            final Object lock = new Object();
            public void run() {
                synchronized (lock) {
                    try {
//                        lock.wait();//使用wait无限期等待，状态为WAITING
                        Thread.sleep(1000);//使用sleep，状态为TIMED_WAITING

                    } catch (InterruptedException e) {

                    }
                }
            }
        });

        thread1.start();
        System.out.println(thread1.getName()+" state :"
                +thread1.getState());
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(thread1.getName()+" state :"
                +thread1.getState());
    }


}
